<?php

class MessageHelper extends Helper {

    private $rule;
    private $_field;

    public function __construct() {
        $this->rule = array(
            'm_uid' => array('require', '接收人不能为空'),
            'm_content' => array('require', '发送内容不能为空')
        );
        $this->_field = array('m_id', 'm_from', 'm_uid', 'm_name', 'm_content', 'm_dateline', 'm_oid', 'm_info');
        $this->LoadResurces('push/class.umeng');
    }
    
    public function sendSingle($data){
        $validator = Validator::checkRule($data, $this->rule);
        if (!$validator['pass']) {
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $model = $this->LoadModel('Public');
        $data['m_dateline'] = NOW_TIME;
        $do = $model->save('message', $data);
        if($do){
            $umeng = new Umeng();
            //ANDROID
            $result = $umeng->androidSend(array(
                'type' => 'customizedcast',
                'alias_type' => 'user',
                'alias' => $data['m_uid'],
                'display_type' => 'notification',
                'ticker' => $data['m_title'],
                'title' => $data['m_title'],
                'text' => $data['m_content']
            ));
            //IOS
            $result = $umeng->iosSend(array(
                'type' => 'customizedcast',
                'alias_type' => 'user',
                'alias' => $data['m_uid'],
                'display_type' => 'notification',
                'ticker' => $data['m_title'],
                'title' => $data['m_title'],
                'text' => $data['m_content']
            ));
            //短信(发送给网点负责人)
            $member_info = $model->getOneRow('member', '*', array('`u_id` = ?' => $data['m_uid']));
            $br_principal = $model->getList('member', '*', array('`u_scope` = ?' => '部门级', '`u_dp_type` = ?' => $member_info['u_dp_type'], '`u_dp_id` = ?' => $member_info['u_dp_id'], '`u_status` = ?' => 'ok'));
            $send_mobi = array();
            if(!empty($br_principal)){
                foreach($br_principal as $k => $v){
                    if(!empty($v['u_mobile'])){
                        $send_mobi[] = $v['u_mobile'];
                    }
                }
            }
            if(!empty($send_mobi)){
                $this->LoadResurces('sms/class.sms');
                $sms = new Sms();
                $sms_send = $sms->sendsms($send_mobi, $data['m_content']);
            }
            
            return array('success' => true, 'msg' => '处理成功');
        }
        return array('success' => false, 'msg' => '处理失败');
    }
    /**
     * 消息列表
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function messageList($where, $page = 1, $limit = PAGE_SIZE, $param = array()){
        $model = $this->LoadModel('Public'); 
        $data['count'] = $model->getRowCount('message', $where);
        if(!empty($data['count'])){
            $data['all'] = $model->getList('message', $this->_field, $where, array($page, $limit), NULL, array('m_id DESC'));
            if(!empty($data['all'])) foreach($data['all'] as $k => $v){
                $data['all'][$k]['m_info'] = unserialize($v['m_info']);
            }
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }
    /**
     * 消息保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function messageSave($data, $where){
        if(is_numeric($where)){
            $where = array('`m_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public'); 
        if(empty($where)){
            $id = $model->save('message', $data);
            if($id){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $model->update('message', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 统计条数
     * @param type $where
     * @return type
     */
    public function messageCount($where){
        if(is_numeric($where)){
            $where = array('`m_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public'); 
        $data = $model->getRowCount('message', $where);
        return $data;
    }
    
}